package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Reimbursement;
import com.gx.mxbbx.service.TotalService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
public class TotalController {
    @Autowired
    private TotalService totalService;

    @RequestMapping("/getTotalList")
    public String getTotalList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String fullName = request.getParameter("context");
        String countyId = request.getParameter("countyIds");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Reimbursement> pageInfo = totalService.getTotalList(fullName, countyId, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", fullName);
        model.addAttribute("countyId", countyId);
        return "/total/total_list";
    }

    @RequestMapping("/output")
    public void output(HttpServletRequest request, HttpServletResponse response) throws IOException {

        XSSFWorkbook wb = totalService.show();
        String fileName = "报销统计信息.xlsx";
        OutputStream outputStream = null;
        fileName = URLEncoder.encode(fileName, "UTF-8");
        //设置ContentType请求信息格式
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
